package i2.application.checklist.dao.jpa.impl;

import i2.application.checklist.dao.QuestionDao;
import i2.application.checklist.entities.Categorie;
import i2.application.checklist.entities.Question;
import i2.application.fmw.dao.jpa.JPADAO;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

public class QuestionDaoImpl extends JPADAO<Question> implements QuestionDao {

	//FIXME mettre les @repository
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Question> findAllByCategorie(Categorie categorie) {
		List<Question> questionList = null;
		Query query = entityManager.createNamedQuery("Question.findAllByCategorie"); 
		query.setParameter("categorieId", categorie.getId());
		questionList = query.getResultList();

		logger.debug("findAllByCategorie : {}",questionList); 
		return questionList;
	}

	@Override
	public boolean hasReponses(Question question) {
		Query query = entityManager.createNamedQuery("Question.hasReponses");
		query.setParameter("id", question.getId());
		Object hasLivraisons = null;
		try {
			hasLivraisons = query.getSingleResult();
			logger.debug("la question a des responses ? : {}", hasLivraisons);
		} catch (NoResultException e) {
			return false;
		}
		return true;
	}
	
}
